package com.huang.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.eduservice.entity.EduTeacher;
import com.huang.eduservice.entity.vo.EduTeacherQuery;
import com.huang.eduservice.service.EduTeacherService;
import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-16
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;


    @GetMapping("list")
    public R getlist() {
        List<EduTeacher> list = eduTeacherService.list(null);


        return R.ok().data("stulist", list);
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public R delete(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        if (eduTeacherService.removeById(id)) {
            return R.ok();
        } else {
            return R.error();
        }

    }

    @GetMapping("pageteacher/{current}/{limit}")
    @ApiOperation("讲师分页")
    public R getPage(@PathVariable Integer current, @PathVariable Integer limit) {
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        IPage<EduTeacher> page = eduTeacherService.page(eduTeacherPage, null);
        return R.ok().data("page", page);
    }


    @PostMapping("pageteacherCondition/{current}/{limit}")
    @ApiOperation("讲师分页条件查询")
    public R pageteacherCondition(@PathVariable Integer current, @PathVariable Integer limit
            , @RequestBody(required = false) EduTeacherQuery query) {
        //@RequestBody(required = false) 使用json传递数据必须是post提交  把数据分装到独享里面 false表示这个数据可以没有为空
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        QueryWrapper wrapper = new QueryWrapper();
        Integer level = query.getLevel();
        String name = query.getName();
        String begin = query.getBegin();
        String end = query.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.eq("gmt_modified", end);
        }
        IPage page = eduTeacherService.page(eduTeacherPage, wrapper);
        List records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("records", records).data("total", total);
    }

    @PostMapping("addteacher")
    @ApiOperation("添加讲师")
    public R addteacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("查讲师")
    @GetMapping("getteacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @PostMapping("updateTeacher")
    @ApiOperation("更改讲师")
    public R updateteacher(@RequestBody EduTeacher eduTeacher) {
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }

    }
}

